package com.wt.admin.config;

import com.wt.admin.config.cache.Cache;
import com.wt.admin.config.cache.CacheManager;
import com.wt.admin.domain.dom.SessionCodeDO;
import com.wt.admin.domain.entity.sys.SysSettingEntity;
import com.wt.admin.domain.vo.UserVO;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class GlobalBeanConfig {

    /**
     * 系统国际化文件配置
     * @return MessageSource
     */
    @Bean(name = "messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        // msg 是文件前缀
        messageSource.setBasename("classpath:i18n/msg");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    /**
     * user缓存配置
     * @return
     */
    @Bean
    public Cache<Long, UserVO> userCache(){
        return CacheManager.getCache("user");
    }

    @Bean
    public Cache<String, SessionCodeDO> sessionCodeCache(){
        return CacheManager.getCache("sessionCode");
    }

    /**
     * 缓存配置
     * @return
     */
    @Bean
    public Cache<String, SysSettingEntity> settingCache(){
        return CacheManager.getCache("setting");
    }

    /**
     * 异步线程池配置（用于系统内部）
     * @return
     */
    @Bean(name = "newAsyncExecutor")
    public Executor newAsync() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        taskExecutor.setCorePoolSize(20);
        // 线程池维护线程的最大数量，只有在缓冲队列满了以后才会申请超过核心线程数的线程
        taskExecutor.setMaxPoolSize(50);
        //缓存队列
        taskExecutor.setQueueCapacity(50);
        //允许的空闲时间，当超过了核心线程数之外的线程在空闲时间到达之后会被销毁
        taskExecutor.setKeepAliveSeconds(120);
        //异步方法内部线程名称
        taskExecutor.setThreadNamePrefix("定时任务-");
        //拒绝策略
        taskExecutor.initialize();
        return taskExecutor;
    }

}
